package com.k8sops.backend.controller;

import com.k8sops.backend.common.Result;
import com.k8sops.backend.entity.Cluster;
import com.k8sops.backend.service.ClusterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cluster")
public class ClusterController {

    @Autowired
    private ClusterService clusterService;

    /**
     * 获取集群列表
     */
    @GetMapping("/list")
    public Result<List<Cluster>> list() {
        List<Cluster> clusters = clusterService.listClusters();
        return Result.success(clusters);
    }

    /**
     * 获取集群详情
     */
    @GetMapping("/detail/{id}")
    public Result<Cluster> detail(@PathVariable Long id) {
        Cluster cluster = clusterService.getById(id);
        return Result.success(cluster);
    }

    /**
     * 添加集群
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody Cluster cluster) {
        boolean success = clusterService.addCluster(cluster);
        return Result.success(success);
    }

    /**
     * 更新集群
     */
    @PutMapping("/update")
    public Result<Boolean> update(@RequestBody Cluster cluster) {
        boolean success = clusterService.updateCluster(cluster);
        return Result.success(success);
    }

    /**
     * 删除集群
     */
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = clusterService.deleteCluster(id);
        return Result.success(success);
    }

    /**
     * 测试集群连接
     */
    @GetMapping("/test/{id}")
    public Result<Boolean> testConnection(@PathVariable Long id) {
        boolean success = clusterService.testConnection(id);
        return Result.success(success);
    }
}
